package com.starmark.gateway.auth.provider.service.impl;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starmark.core.framework.base.AbstractBaseService;
import com.starmark.core.framework.service.CommonIdToNameService;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.core.framework.util.RedisUtils;
import com.starmark.gateway.auth.api.service.ISysMenuApiService;
import com.starmark.gateway.auth.provider.entity.GatewayAuthResource;
import com.starmark.gateway.auth.provider.mapper.GatewayAuthResourceMapper;
import com.starmark.gateway.auth.provider.service.IGatewayAuthResourceService;
import com.starmark.gateway.auth.provider.vo.GatewayAuthResourceVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统菜单 服务实现类
 * </p>
 *
 * @author starmark
 * @since 2019-04-21
 */
@RestController
@Service
public class GatewayAuthResourceServiceImpl extends AbstractBaseService<GatewayAuthResourceMapper, GatewayAuthResource> implements ISysMenuApiService, IGatewayAuthResourceService, CommonIdToNameService {
    private static final String STARMARK_GATEWAY_RES = "starmark:gateway:res:";
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void buildPageSearch(Map<String, Object> params, QueryWrapper<GatewayAuthResource> eWrapper) {
        if (params.get("keyword") != null && StrUtil.isNotEmpty(params.get("keyword") + "")) {
            eWrapper.like("name", params.get("keyword") + "");
        }
        //查询工程ID
        eWrapper.eq("project_code", params.get("projectCode") + "");


    }

    @Override
    public void deleteResource(String resourceId) {
        GatewayAuthResource gatewayAuthResource = this.getById(resourceId);
        Assert.notNull(gatewayAuthResource, "权限资源不存在：" + resourceId);
        redisUtils.del(STARMARK_GATEWAY_RES + gatewayAuthResource.getProjectCode() + ":" + gatewayAuthResource.getId());
        this.removeById(resourceId);
    }

    /**
     * 通过组织ID,查找相关权限资源ID
     *
     * @param orgIds 组织数据
     * @return 权限资源ID
     */
    @Override
    public Set<String> getPermissionIdsByOrgId(List<String> orgIds) {
        return this.getBaseMapper().getPermissionIdsByOrgId(orgIds);
    }


    @Override
    public List<GatewayAuthResource> getResourceTree(String projectCode) {
        QueryWrapper<GatewayAuthResource> eWrapper = new QueryWrapper<>();
        //查询工程ID
        eWrapper.eq("project_code", projectCode);
        return this.list(eWrapper);
    }

    @Override
    public void saveOrUpdateResource(GatewayAuthResourceVo gatewayAuthResourceVo) {
        GatewayAuthResource gatewayAuthResource = BeanMapper.map(gatewayAuthResourceVo, GatewayAuthResource.class);
        this.saveOrUpdate(gatewayAuthResource);
        BeanMapper.map(gatewayAuthResource, GatewayAuthResourceVo.class);
        //设置缓存
        redisUtils.set(STARMARK_GATEWAY_RES + gatewayAuthResource.getProjectCode() + ":" + gatewayAuthResource.getId(), JSONObject.toJSONString(gatewayAuthResourceVo));

    }


    @Override
    public String getNameById(String id) {
        GatewayAuthResource gatewayAuthResource = this.getById(id);
        return Optional.of(gatewayAuthResource).map(GatewayAuthResource::getName).orElse("");
    }

    @Override
    public Set<String> getMenuIdsByRoleIds(Set<String> roleIds) {
        return this.getBaseMapper().getMenuIdsByRoles(roleIds);

    }

    @Override
    public List<GatewayAuthResource> getAuthMenu(Set<String> roleIds) {
        Set<String> meuIds = this.getMenuIdsByRoleIds(roleIds);
        return this.getBaseMapper().selectBatchIds(meuIds);
    }

    @Override
    public Set<String> getPermissionByUrl(String checkUrl, String methodType) {
        QueryWrapper<GatewayAuthResource> eWrapper = new QueryWrapper<>();
        eWrapper.eq("path", checkUrl);
        if (StrUtil.isNotEmpty(methodType)) {
            eWrapper.like("methodType", methodType);
        }

        List<GatewayAuthResource> list = this.list(eWrapper);
        return list.stream().map(GatewayAuthResource::getId).collect(Collectors.toSet());
    }

    @Override
    public boolean overload() {
        List<GatewayAuthResource> resources = this.list();
        resources.forEach(resource -> {
            GatewayAuthResourceVo resourceVo = BeanMapper.map(resource, GatewayAuthResourceVo.class);

            redisUtils.set(STARMARK_GATEWAY_RES + resource.getProjectCode() + ":" + resource.getId(), JSONObject.toJSONString(resourceVo));

        });

        return true;
    }


}
